package cn.fsd.controller;

import cn.fsd.domain.Storeroom;
import cn.fsd.query.delivery.StoreroomQuery;
import cn.fsd.service.impl.StoreRoomServiceImpl;
import cn.fsd.util.Constants;
import cn.fsd.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/storeroom")
public class StoreRoomController {

  @Autowired
  StoreRoomServiceImpl storeRoomService;

  @PostMapping
  @ApiOperation(value = "新增中心库房信息")
  public ResultBean addStoreRoom(@RequestBody Storeroom storeroom) {
    if (StringUtils.isEmpty(storeroom.getStoreroom_name())) {
      return ResultBean.error("库房名称不能为空");
    } else if (StringUtils.isEmpty(storeroom.getStoreroom_place())) {
      return ResultBean.error("库房地址不能为空");
    } else if (StringUtils.isEmpty(storeroom.getStoreroom_manager())) {
      return ResultBean.error("库管员名字不能为空");
    } else if (StringUtils.isEmpty(storeroom.getStoreroom_phone())) {
      return ResultBean.error("库管员电话不能为空");
    } else if (storeroom.getStoreroom_level() == null) {
      return ResultBean.error("库房级别不能为空");
    } else if (!Objects.equals(storeroom.getStoreroom_level(), Constants.CENTER)
        && !Objects.equals(storeroom.getStoreroom_level(), Constants.SUBSTATION)) {
      return ResultBean.error("库房级别必须是中心库房或者分站库房");
    }
    Storeroom s = storeRoomService.getStoreRoomByName(storeroom.getStoreroom_name());
    if (s != null) {
      return ResultBean.error("库房名称重复");
    }
    return ResultBean.success((Object)storeRoomService.addStoreRoom(storeroom));
  }

  @GetMapping("/{id}")
  @ApiOperation(value = "根据id获取库房")
  public ResultBean getStoreroomById(@PathVariable("id")String id){
    return ResultBean.success(storeRoomService.getStoreRoomByID(id));
  }

  @PostMapping("/page")
  @ApiOperation(value = "分页查询库房")
  public ResultBean findStoreroomByPage(@RequestBody StoreroomQuery query) {
    return ResultBean.success(storeRoomService.findStoreRoomByPage(query));
  }

  @DeleteMapping("/{id}")
  @ApiOperation(value = "根据id删除库房")
  public ResultBean delStoreroomById(@PathVariable("id") String id) {
    if(-1==storeRoomService.delStoreroomByID(id)){
      return ResultBean.error("库房内有商品无法删除");
    }
    return ResultBean.success();
  }

  @PutMapping
  @ApiOperation(value = "更新库房信息")
  public ResultBean updateStoreroom(@RequestBody Storeroom storeroom) {
    if (StringUtils.isEmpty(storeroom.getStoreroom_id())) {
      return ResultBean.error("库房id不能为空");
    } else if (StringUtils.isEmpty(storeroom.getStoreroom_name())) {
      return ResultBean.error("库房名称不能为空");
    } else if (StringUtils.isEmpty(storeroom.getStoreroom_place())) {
      return ResultBean.error("库房地址不能为空");
    } else if (StringUtils.isEmpty(storeroom.getStoreroom_manager())) {
      return ResultBean.error("库管员名字不能为空");
    } else if (StringUtils.isEmpty(storeroom.getStoreroom_phone())) {
      return ResultBean.error("库管员电话不能为空");
    } else if (storeroom.getStoreroom_level() == null) {
      return ResultBean.error("库房级别不能为空");
    } else if (!Objects.equals(storeroom.getStoreroom_level(), Constants.CENTER)
        && !Objects.equals(storeroom.getStoreroom_level(), Constants.SUBSTATION)) {
      return ResultBean.error("库房级别必须是中心库房或者分站库房");
    }
    Storeroom s = storeRoomService.getStoreRoomByName(storeroom.getStoreroom_name());
    if (s != null&&!Objects.equals(s.getStoreroom_id(),storeroom.getStoreroom_id())) {
      return ResultBean.error("库房名称重复");
    }
    storeRoomService.updateStoreroom(storeroom);
    return ResultBean.success();
  }
}
